package io.renren.modules.hos.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.utils.ShiroUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.hos.entity.DoctorEntity;
import io.renren.modules.hos.entity.PatientEntity;
import io.renren.modules.hos.form.AuthDoctorForm;
import io.renren.modules.hos.form.AuthPatientForm;
import io.renren.modules.hos.form.HosForgetForm;
import io.renren.modules.hos.service.DoctorService;
import io.renren.modules.hos.service.PatientService;
import io.renren.modules.hos.vo.HosUserVo;
import io.renren.modules.sys.service.SysCaptchaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.hos.entity.HosUserEntity;
import io.renren.modules.hos.service.HosUserService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author ryf
 * @email 2559228480@qq.com
 * @date 2021-11-28 20:33:30
 */
@RestController
@RequestMapping("hos/user")
public class HosUserController {
    @Autowired
    private HosUserService userService;
    @Autowired
    private SysCaptchaService sysCaptchaService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private PatientService patientService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("hos:user:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("hos:user:info")
    public R info(@PathVariable("id") Long id){
		HosUserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("hos:user:save")
    public R save(@RequestBody HosUserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("hos:user:update")
    public R update(@RequestBody HosUserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("hos:user:delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 忘记密码
     * @return
     */
    @PostMapping("/password")
    public R ForgetPassword(@RequestBody HosForgetForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);

        boolean captcha = sysCaptchaService.validate(form.getUuid(), form.getCaptcha());
        if(!captcha){
            return R.error("验证码不正确");
        }

        HosUserEntity user = userService.queryUserByPhone(form.getPhone());

        user.setPassword(form.getPassword());
        boolean flag = userService.updateById(user);
        if (flag){
            return R.ok();
        }
        else {
            return R.error("修改失败");
        }
    }

    /**
     *  获取用户信息
     */
    @GetMapping("/userInfo")
    public R userInfo(){
        HosUserVo hosUserVo = userService.selectUserById(ShiroUtils.getHosUserId());

        return R.ok().put("data", hosUserVo);
    }

    /**
     *  用户病人身份认证
     */
    @RequestMapping("/authPatient")
    public R userIdentityAuth(@RequestBody AuthPatientForm authForm){
        boolean flag = userService.authPatient(authForm);
        if (flag){
            return R.ok().put("data", "认证成功");
        }else {
            return R.error().put("data", "认证失败");
        }
    }

    /**
     *  用户医生身份认证
     */
    @PostMapping("/authDoctor")
    public R userIdentityAuth(@RequestBody AuthDoctorForm authDoctorForm){
        boolean flag = userService.authDoctor(authDoctorForm);
        if (flag){
            return R.ok().put("data", "认证成功");
        }else {
            return R.error().put("data", "认证失败");
        }
    }


    @RequestMapping("/userDetailedInfo")
    public R userDetailedInfo(){
        HosUserEntity user = userService.getById(ShiroUtils.getHosUserId());

        if (user.getAuthIdentity() == 0){
            R r = new R();
            r.put("data", user);
            r.put("msg", "未认证");
            return r;
        } else if (user.getAuthIdentity() == 1){
            PatientEntity patientEntity = patientService.selectByUserId(user.getId());
            return R.ok().put("data", patientEntity);
        }else {
            DoctorEntity doctorEntity = doctorService.selectByUserId(user.getId());
            return R.ok().put("data", doctorEntity);
        }
    }


}
